<?php

	if($_GET['operation']=="add" or $_GET['operation']=="edit")
	{
		if($_POST['act'])
		{	
			unset($_GET['operation']);
			unset($_GET['s']);
			unset($_GET['m']);
			//添加
			if($_POST["act"]=='save')
			{	
				if($_POST["pid"]==0)
				{
					$sql="select max(id) as id from ".TAG." where id<9999";
					$db->query($sql);
					$id=$db->fetchField("id");
					if(!$id)
						$id=1000;
					else
						$id=substr($id*1000,0,4)+1;
				}
				else
				{
					$s=$_POST["pid"]."00";
					$b=$_POST["pid"]."99";
					$sql="select max(id) as id from ".TAG." where id>$s and id<$b";
					$db->query($sql);
					$id=$db->fetchField("id");
					if(!$id)
						$id=$_POST["pid"]."01";
					else
						$id=$id+1;
				}
				foreach(explode("\r\n",$_POST['tags']) as $tagsv)
				{
					if(!empty($tagsv))
					{
						$sql="insert into ".TAG." (`id`,`tags`,`status`,`logo`,`logo_large`,create_user) values 
							('$id','$tagsv','$_POST[status]','$_POST[logo]','$_POST[logo_large]','$_SESSION[ADMIN_USER]')";
						$db->query($sql);
						$id+=1;
					}
				}
			}
			//修改
			if($_POST["act"]=='edit' and is_numeric($_POST['id']))
			{
				
				if($_POST["pid"]!=substr($_POST['id'],0,strlen($_POST['id'])-2) and strlen($_POST['id'])>4 )
				{
					if($_POST["pid"]==0)
					{
						$sql="select max(id) as id from ".TAG." where id<9999";
						$db->query($sql);
						$id=$db->fetchField("id");
						if(!$id)
							$id=1000;
						else
							$id=substr($id*1000,0,4)+1;
					}
					else
					{
						$s=$_POST["pid"]."00";
						$b=$_POST["pid"]."99";
						$sql="select max(id) as id from ".TAG." where id>$s and id<$b";
						$db->query($sql);
						$id=$db->fetchField("id");
						if(!$id)
							$id=$_POST["pid"]."01";
						else
							$id=$id+1;
					}
				}
				else
					$id=$_POST['id'];

				
				$sql="update ".TAG." set id='$id', tags='$_POST[tags]',status='$_POST[status]' ,logo='$_POST[logo]',logo_large='$_POST[logo_large]' where id='".$_POST['id']."'";
				$db->query($sql);
				
				
				if($_POST["pid"]!=substr($_POST['id'],0,strlen($_POST['id'])-2))
				{
					//如果当前类别下面带有子类别把子类别一起移过去
					$s=$_POST['id']."00";
					$b=$_POST['id']."99";
					$sql="update ".TAG." set id=replace(id,$_POST[id],$id) where id like '$_POST[id]%'";
					$re=$db->query($sql);
				}
				unset($_GET['editid']);
			}
			$getstr=implode('&',convert($_GET));
			msg("?m=product&s=product_tags.php&$getstr");
		}
		if($_GET['editid'] and is_numeric($_GET['editid']))
		{
			$sql="select * from ".TAG." where id='$_GET[editid]'";
			$db->query($sql);
			$re=$db->fetchRow();
			$re['cid']=substr($re['id'],0,-2);
			$tpl->assign("re",$re);
		}
		$sql="select * from ".TAG." where id<9999 order by displayorder,id";
		$db->query($sql);
		$de=$db->getRows();
		foreach($de as $key=>$val)
		{
			$sql="select * from ".TAG." where id < '".$val['id']."99' and id > '".$val['id']."00' order by displayorder,id";
			$db->query($sql);
			$a=$db->getRows();
			foreach($a as $ke=>$va)
			{
				$sql="select * from ".TAG." where id < '".$va['id']."99' and id > '".$va['id']."00' order by displayorder,id";
				$db->query($sql);
				$a[$ke]['stags']=$db->getRows();
			}
			$de[$key]['stags']=$a;
		}
		
	}
	else
	{
		if($_GET['delid'])
		{
			$sql="delete from ".TAG."  where id like '$_GET[delid]%' ";
			$db->query($sql);
			msg("?m=product&s=product_tags.php");
		}
		if($_POST['act']=='op')
		{
			if($_POST['name'])
			{
				foreach($_POST['name'] as $key=>$list)
				{
					if(!empty($list))
					{
						$displayorder=$_POST['displayorder'][$key];
						$displayorder=$displayorder?$displayorder*1:"0";
						
						$db->query("update ".TAG." set tags='$list',displayorder='$displayorder' where id='$key'");		
					}
				}
			}
			msg("?m=product&s=product_tags.php");
		}
	
		if($_GET['id'])
		{
			$id=$_GET['id']*1;
			
		}
		
			
		$sql="select * from ".TAG." where 1 and id<9999 order by displayorder,id";
		$db->query($sql);
		$de=$db->getRows();
		foreach($de as $k=>$v)
		{
			
			$tsql=" and id < '".$v['id']."99' and id >'".$v['id']."00' ";
			$sql="select * from ".TAG." where 1 $tsql order by displayorder,id";
			$db->query($sql);
			$a=$db->getRows();
			foreach($a as $ks=>$vs)
			{
				
				$tsql=" and id < '".$vs['id']."99' and id >'".$vs['id']."00' ";
				$sql="select * from ".TAG." where 1 $tsql order by displayorder,id";
				$db->query($sql);
				$b=$db->getRows();
				foreach($b as $kss=>$vss)
				{

					$tsql=" and id < '".$vss['id']."99' and id >'".$vss['id']."00' ";
					$sql="select * from ".TAG." where 1 $tsql order by displayorder,id";
					$db->query($sql);
					$c=$db->getRows();
					$b[$kss]['stags']=$c;
						
				}
				$a[$ks]['stags']=$b;
				
			}
			$de[$k]['stags']=$a;
		}
	}
	$tpl->assign("de",$de);
	$tpl->display("product_tags.htm");
?>